require.config({
    paths: {
        vue: "../encaptool/encvue",
        iview: "../utiltool/iview/iview",
        jquery: "../utiltool/jquery/jquery.min",
        router: "../encaptool/encarouter",
        axios: "../encaptool/encaxios"
    },
    shim: {
        iview: ["vue"]
    }
});
require(["vue", "iview", "jquery", "router", "axios"], function (
    Vue,
    iview,
    $,
    Router,
    axios
) {
    Vue.use(iview);
    new Vue({
        el: "#to_complete",
        data() {
            return {
                modal_loading: false,
                index:null,
                calmodal1:false,
                freightId:'',
                orderid:'',
                value4:null,
                value5:null,
                loading: false,
                columns: [
                    {
                        title: "序号",
                        type:'index',
                        align: "center",
                        width:65,
                    },
                    {
                        title: "送货单号",
                        key: "freightId",
                        align: "center",
                        width:150
                    },
                    {
                        title: "订单号",
                        key: "orderid",
                        align: "center",
                        width:150   
                    },
                    {
                        title: "配送车辆",
                        key: "truckid",
                        align: "center",
                        width:150
                    },
                    {
                        title: "手机号",
                        key: "phone",
                        align: "center",
                        width:150
                    },
                    {
                        title: "订单产生日期",
                        key: "createdate",
                        align: "center",
                        width:150
                    },
                    {
                        title: "目的地址",
                        key: "bourn",
                        align: "center",
                        width:385
                    },
                    {
                        title: "公里(约)",
                        key: "kilometre",
                        align: "center",
                        "sortable": true,
                        width:110
                    },
                    {
                        title: "是否加急",
                        key: "isEmergency",
                        align: "center",
                        width:150,
                        filters: [{
                                label: '加急单',
                                value: 0
                            },
                            {
                                label: '正常单',
                                value: 1
                            }
                        ],
                        filterMultiple: false,
                        filterMethod(value, row) {
                            if (value === 0) {
                                return row.isEmergency >= 1;
                            } else if (value === 1) {
                                return row.isEmergency <= 0;
                            }
                        },
                        render: (h, params) => {
                            const row = params.row;
                            const color = row.isEmergency === 0 ? 'blue' : row.isEmergency === 1 ? 'red' : 'green';
                            const text = row.isEmergency === 0 ? '正常单' : row.isEmergency === 1 ? '加急单' : '无状态';
                            return h('Tag', {
                                props: {
                                    type: 'dot',
                                    color: color
                                }
                            }, text);
                        }
                    },
                    {
                        title: "操作",
                        key: "operation",
                        align: "center",
                        width: 170,
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: {
                                        type: 'primary'
                                    },
                                    style: {
                                        marginRight: '5px'
                                    },
                                    on: {
                                        click: () => {
                                            this.show(params.index)
                                        }
                                    }
                                }, '详细'),
                                h('Button', {
                                    props: {
                                        type: 'info'
                                    },
                                    on: {
                                        click: () => {
                                            //this.remove(params.index)
                                            this.index=params.index;
                                            this.freightId=this.data[params.index].freightId;
                                            this.orderid=this.data[params.index].orderid;
                                            this.calmodal1=true
                                        }
                                    }
                                }, '取消')
                            ]);
                        }
                    }
                ],
                cityList: [
                    {
                        value: '商品破损',
                        label: '商品破损'
                    },
                    {
                        value: '商品错发、漏发',
                        label: '商品错发、漏发'
                    },
                    {
                        value: '不喜欢、拍错',
                        label: '不喜欢、拍错'
                    },
                    {
                        value: '发票问题',
                        label: '发票问题'
                    },
                    {
                        value: '商品质量问题',
                        label: '商品质量问题'
                    },
                    {
                        value: '未按约定时间发货',
                        label: '未按约定时间发货'
                    }
                ],
                model1: '',
                data: [],
                showIndex: true,
                setting: {
                    method: "GET",
                    url: "/YF/public/api/getwaitdata"
                },
                setting1: {
                    method: "GET",
                    url: "/YF/public/api/getorderfind/"
                },
                setting2: {
                    method: "GET",
                    url: "/YF/public/api/cancelorder/"
                },
                // 初始化信息总条数
                dataCount: 0,
                // 每页显示多少条
                pageSize: 50,
                // 每页信息
                historyData: [],
            };
        },
        mounted() {
            this.getwaitdata();
        },
        methods: {
            //导出数据excle.csv的
            exportData(type) {
                var myDate = new Date();//获取系统当前时间
                var year = myDate.getFullYear();
                var month = myDate.getMonth()+1;
                var day = myDate.getDate();
                var hour = myDate.getHours();
                var minute = myDate.getMinutes();
                var second = myDate.getSeconds();
                if (type === 1) {
                    this.$refs.table.exportCsv({
                        filename: +year+'年'+month+'月'+day+'日 '+hour+':'+minute+':'+second,
                        original: false,
                        columns: this.columns.filter((col, index) => index>0 && index < 7),
                        data: this.data.filter((data, index) => index < this.data.length)
                    });
                }
            },
            //获取待完成订单
            getwaitdata() {
                this.loading = true;
                axios.call(this, function (res) {
                    if (res.code === 0) {
                        this.data = res.data;
                        this.dataCount = res.data.length;
                        if(this.dataCount < this.pageSize) {
                            this.historyData = this.data;
                        }else {
                            this.historyData = this.data.slice(0, this.pageSize);
                        }
                        this.loading = false;
                    } else {
                        this.$Message.info("未查到数据");
                        this.loading = false;
                    }
                }.bind(this), {
                    setting: this.setting
                });
            },
            //获取搜索的订单
            getfind() {
                this.loading = true;
                this.setting1.url += this.trim;
                if (this.trim == null) {
                    this.$Message.info("请输入相应查询信息");
                    this.loading = false;
                } else {
                    axios.call(this, function (res) {
                        if (res.code === 0) {
                            this.historyData = res.data;
                            this.loading = false;
                            this.setting1.url = '/YF/public/api/getorderfind/';
                        } else {
                            this.$Message.info("未查到数据");
                            this.setting1.url = '/YF/public/api/getorderfind/';
                            this.loading = false;
                        }
                    }.bind(this), {
                        setting: this.setting1
                    });
                }
            },
            //查询信息框
            show(index) {
                this.$Modal.info({
                    title: '详细信息',
                    content: `<div style="font-size:15px"><b>送货单号：${this.data[index].freightId == null ? '暂无数据' : this.data[index].freightId}<br>
                    生产单号：${this.data[index].createdate == null ? '暂无数据' : this.data[index].createdate}<br>
                    目的地址：${this.data[index].bourn == null ? '暂无数据' : this.data[index].bourn}<br>
                    是否加急：${this.data[index].isEmergency == null ? '无状态' : (this.data[index].isEmergency === 0 ? '正常单' : this.data[index].isEmergency === 1 ? '加急单' : '无状态')}<br>
                    板材：${this.data[index].board == null ? '暂无数据' : this.data[index].board}<br>
                    车牌号：${this.data[index].truckid == null ? '暂无数据' : this.data[index].truckid}<br>
                    规格/楞型：${this.data[index].format == null ? '暂无数据' : this.data[index].format}</b></div>`
                })
            },
            //取消订单
            remove(index) {
                this.freightId=this.data[index].freightId;
                this.setting2.url += this.freightId+"/"+this.model1;
                //this.setting2.url += this.data[index].freightId+"/"+this.data[index].orderid+"/"+this.model1;
                axios.call(this, function (res) {
                    if (res.code === 0) {
                        this.loading = false;
                        this.setting2.url = '/YF/public/api/cancelorder/';
                        this.$options.methods.success.bind(this)();
                        this.data.splice(0, this.data.length);
                        this.$options.methods.getwaitdata.bind(this)();
                        this.value5=null;
                        this.calmodal1 = false;
                    } else {
                        //this.$Message.info("未查到数据");
                        this.setting2.url = '/YF/public/api/cancelorder/';
                        this.$options.methods.info.bind(this)();
                        this.data.splice(0, this.data.length);
                        this.$options.methods.getwaitdata.bind(this)();
                        this.loading = false;
                        this.calmodal1 = false;
                    }
                }.bind(this), {
                    setting: this.setting2
                });
            },
            //分页插件
            changePage (index) {
                var _start = ( index - 1 ) * this.pageSize;
                var _end = index * this.pageSize;
                this.historyData = this.data.slice(_start,_end);
            },
            success (nodesc) {
                this.$Notice.success({
                    title: '取消',
                    desc: nodesc ? '' : '成功取消当前运单：'+this.freightId,
                    duration: 3
                });
            },
            info (nodesc) {
                this.$Notice.info({
                    title: '取消',
                    desc: nodesc ? '' : '未成功取消当前运单：'+this.freightId,
                });
            },
            info1 (nodesc) {
                this.$Notice.info({
                    title: '取消原因',
                    desc: nodesc ? '' : '取消原因不能为空，否则不能取消订单',
                });
                this.calmodal1 = false;
            },
            // ok () {
            //     if(this.model1==null)
            //     {
            //         this.info1();
            //     }else
            //     {
            //         this.remove(this.index);
            //     }     
            // },
            // cancel () {
            //     this.value5=null;
            // },
            del () {
                if(this.model1=='')
                {
                    this.info1();
                }else{
                    this.modal_loading = true;
                    setTimeout(() => {
                        this.modal_loading = false;
                        this.remove(this.index);
                    }, 500);
                }     
            },
        },
        computed: {
            trim: function () {
                if(this.value4==null)
                {
                    this.value4=null;
                }else
                {
                    return  this.value4.replace(/(^\s*)|(\s*$)/g, "");
                }
            },
        }
    });
});